<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/24
 * Time: 10:53
 * Desc: 用于测试数据的测试控制器
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Model;
use app\index\controller\Base;
use app\index\service\WeixinService;

class Test extends Base{

    public function index(){
        $cur_uid = 12312;
        $pay_code = '5270'.sprintf("%04d", $cur_uid);         //会员支付码
        halt($pay_code);

//        halt(date('Y-m-d H:i:s',1524122422));
        echo '<tr><td colspan="4" style="font-size: large;text-align: center">一朵</td></tr><tr><td colspan="4" style="font-size: large;text-align: center">&nbsp;</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">单号 L201805091652185050509913</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">收银员 </td></tr><tr><td colspan="4" style="text-align: center">———————————————</td></tr><tr><td style="width: 25mm;">商品</td><td>数量</td><td>单价</td><td>小计</td></tr><tr style="font-size: smaller;"><td colspan="4" style="text-align: left">百利甜情人</td></tr><tr style="font-size: smaller;"><td style="width: 25mm"></td><td>4</td><td>200</td><td>800</td></tr><tr style="font-size: smaller;"><td colspan="4" style="text-align: left">蛋糕</td></tr><tr style="font-size: smaller;"><td style="width: 25mm"></td><td>1</td><td>0.01</td><td>0.01</td></tr><tr><td colspan="4" style="text-align: center">———————————————</td></tr><tr><td colspan="4" style="text-align: center">———————————————</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">总数量：5 余额：￥800.01</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr><tr><td colspan="4" style="text-align: left">欢迎您再次光临</td></tr><tr><td colspan="4" style="text-align: left">2018-05-09 16:52:18</td></tr>';
    }

    /**
     * @Author  lingyun
     * @Desc    发送模板消息
     */
    public function send_msg(){
        $member = db('user')->where('uid',session('uid'))->find();
        //存在会员的openid就推送模板消息
//        if(!empty($member['openid'])){
            //推送模板消息
            $array = [
                "type"   	  =>1,
                "touser" 	  =>'ohW9z0y7Ia7iILgB9j2Y_otsIXb8',
                "template_id"=>config("mobanid.xiadan")["tempid"],
                "url" 		  =>config("mobanid.xiadan")["url"],
                "topcolor"  =>"#743A3A",
                "data"   =>[
                    "first" 	    =>["value"=>"支付成功",        		"color"=>"#743A3A"],
                    "keyword1"		=>["value"=>'2349239123878137', 		"color"=>"#743A3A"],
                    "keyword2"		=>["value"=>'qwejhqwehuhqe', 		"color"=>"#743A3A"],
                    "keyword3"		=>["value"=>"微信支付","color"=>"#743A3A"],
                    "remark"		=>["value"=>"感谢您的购买，请注意查收商品", 			"color"=>"#743A3A"],
                ]
            ];
            $wein = new WeixinService();
            $rest  = $wein->sendmsg($array);
//        }
        //支付成功，跳转至领取优惠券页面
//        $this->success('支付成功',url('PayComplete/get_coupon')."?out_trade_no=".$order_sn,3);
    }

    /**
     * 递归无限级分类【先序遍历算】，获取任意节点下所有子孩子
     * @param array $arrCate 待排序的数组
     * @param int $parent_id 父级节点
     * @param int $level 层级数
     * @return array $arrTree 排序后的数组
     */
    public function getMenuTree($arrCat, $parent_id = 0, $level = 0){
        static  $arrTree = array(); //使用static代替global
        if( empty($arrCat)) return FALSE;
        $level++;
        foreach($arrCat as $key => $value)
        {
            if($value['parent_id' ] == $parent_id)
            {
                $value[ 'level'] = $level;
                $arrTree[] = $value;
                unset($arrCat[$key]); //注销当前节点数据，减少已无用的遍历
                $this->getMenuTree($arrCat, $value[ 'id'], $level);
            }
        }

        return $arrTree;
    }

    public function get_child(){
        $arrCate = array(  //待排序数组
            array( 'id'=>1, 'name' =>'顶级栏目一', 'parent_id'=>0),
            array( 'id'=>2, 'name' =>'顶级栏目二', 'parent_id'=>0),
            array( 'id'=>3, 'name' =>'栏目三', 'parent_id'=>2),
            array( 'id'=>4, 'name' =>'栏目四', 'parent_id'=>3),
            array( 'id'=>5, 'name' =>'栏目五', 'parent_id'=>4),
            array( 'id'=>6, 'name' =>'栏目六', 'parent_id'=>2),
            array( 'id'=>7, 'name' =>'栏目七', 'parent_id'=>6),
            array( 'id'=>8, 'name' =>'栏目八', 'parent_id'=>6),
            array( 'id'=>9, 'name' =>'栏目九', 'parent_id'=>7),
        );

        $result = $this->getMenuTree($arrCate,2,0);
        halt($result);
    }


    public function test(){
        if(request()->isPost()){
            $order_id = input('266');
            $order = db('order')->where('order_sn','L201805301317011005010213')->find();
            $store = db('store')->where('id',$order['store_id'])->find();

            $order_details = db('order_details')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where('a.order_id',$order['id'])->field('a.id,a.goods_id,a.store_goods_stock_id,a.spec_detail,b.name,a.goods_num,a.final_amount,spec_price')->select();

            //小票内容
            $content = '';

            $content .= '<tr><td colspan="4" style="font-size: large;text-align: center">一朵</td></tr><tr><td colspan="4" style="font-size: large;text-align: center">&nbsp;</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">单号 ' . $order["order_sn"] . '</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">收银员 sd</td></tr><tr><td colspan="4" style="text-align: center">———————————————</td></tr><tr><td style="width: 25mm;">商品</td><td>数量</td><td>单价</td><td>小计</td></tr>';

            foreach($order_details as $k => $v){
                $content .= '<tr style="font-size: smaller;"><td colspan="4" style="text-align: left">' . $v['name'].' '.$v["spec_detail"] . '</td></tr>';
                $content .= '<tr style="font-size: smaller;"><td style="width: 25mm"></td><td>' . $v["goods_num"] . '</td><td>' . floatval($v['spec_price']) . '</td><td>' . $v['final_amount'] . '</td></tr>';


                $stock = db('store_goods_stock')->where('id',$v['store_goods_stock_id'])->find();
                $cur_stock = $stock['stock'] - $v['goods_num'];
                //更新门店库存
                db('store_goods_stock')->where('id',$v['store_goods_stock_id'])->update(['stock'=>$cur_stock]);
                $arr = [
                    'goods_id'=>$v['goods_id'],
                    'goods_spec_id'=>$stock['goods_spec_id'],
                    'title'=>$v['spec_detail'],
                    'order_id'=>$order['id'],
                    'store_id'=>$order['store_id'],
                    'ruku_num'=>0,
                    'tuihuo_num'=>0,
                    'chuku_num'=>$v['goods_num'],
                    'sunhao_num'=>0,
                    'remark'=>$order['order_sn'],
                    'cur_stock'=>$cur_stock,
                    'addtime'=>time(),
                    'order_sn'=>$order['order_sn']
                ];
//                db('store_goods_log')->insert($arr);
            }

            $content .= '<tr><td colspan="4" style="text-align: center">———————————————</td></tr>';

            switch ($order['pay_type']) {
                case 1:
                    $method = '支付宝';
                    break;
                case 2:
                    $method = '微信';
                    break;
                case 3:
                    $method = '余额';
                    break;
                case 4:
                    $method = '现金';
                    break;
                case 5:
                    $method = '银行卡';
                    break;
                default:
                    break;
            }

            $content .= '<tr><td colspan="4" style="text-align: left;font-size: smaller">总数量：' . $order["goods_num"] . ' ' . $method . '：￥' . $order["final_amount"] . '</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr>';

            $member = db('user')->where('uid',1)->find();
//            $content .= '<tr><td colspan="4" style="text-align: left;font-size: smaller">会员卡：52700005      余额：123.00</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr>';

            if(!empty($member)){            //有会员信息
                $content .='<tr><td colspan="4" style="text-align: left;font-size: smaller">会员卡：' . $member["pay_code"] . '     余额：￥' . $member["user_money"] . '</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr>';
            }

            $content .='<tr><td colspan="4" style="text-align: left;font-size: smaller">折扣：暂无</td></tr><tr><td colspan="4" style=";text-align: center">&nbsp;</td></tr>';



            $content .= '<tr><td colspan="4" style="text-align: left">' . '欢迎您再次光临' . '</td></tr>';
            $content .= '<tr><td colspan="4" style="text-align: left">' . date('Y-m-d H:i:s') . '</td></tr>';

            $this->success('成功','',$content);
        }else{
            return $this->fetch('index');
        }
    }

}